/*****************************************************************************
 *
 * HUSONG PROPRIETARY INFORMATION
 *
 * The information contained herein is proprietary to HUSONG
 * and shall not be reproduced or disclosed in whole or in part
 * or used for any design or manufacture
 * without direct written authorization from HUSONG.
 *
 * Copyright (c) 2015 by HUSONG. All rights reserved.
 *
 *****************************************************************************/
package com.broker.provider.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.send.broker.cons.OrderCons;
import me.send.broker.entity.Broker;
import me.send.broker.entity.User;
import me.send.broker.entity.base.Page;
import me.send.broker.pojo.BrokerBean;
import me.send.broker.pojo.Data;
import me.send.broker.pojo.OrderInfoBean;
import me.send.broker.pojo.OrderInfoV1_1Bean;
import me.send.broker.pojo.OrderInfoV1_1_1Bean;
import me.send.broker.pojo.OrderInfoV1_1_2Bean;
import me.send.broker.pojo.OrderMap;
import me.send.broker.pojo.TaskBean;
import me.send.broker.service.OrderService;
import me.send.framework.util.HelperUtil;

import com.broker.provider.base.BaseService;

/**
 * @author maxmin
 *
 * @date 2015年8月4日 下午4:59:16
 */
public class OrderServiceImpl extends BaseService implements OrderService {

	private static final long LIMET_COMPET_TIME = 120l;
	private static final int HAS_AUTHENED = 2;
	private static final int IS_NOT_AUTHENED = 0;
	private static final int AUTHENING = 1;

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.OrderService#competing(int, int)
	 */
	/**
	 * 经纪人抢单功能 1、修改订单状态。2、发送通知给用户 订单状态 2为已抢单
	 */
	public int competing(int popid, int orderid, String longitude,
			String latitude) {
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("status", OrderCons.COMPETED);
			params.put("popid", popid);
			params.put("orderid", orderid);
			params.put("createtime", HelperUtil.getCurrentDate());
			params.put("updatetime", HelperUtil.getCurrentDate());
			params.put("longitude", longitude);
			params.put("latitude", latitude);
			params.put("preStatus", 1);
			params.put("orderid", orderid);
			Map<String, Object> ret = orderMapper
					.getOrderCountByBrokerId(params);

			String createtime = ret.get("createtime")==null?null:ret.get("createtime").toString();
			long dur = 181l;
			if (createtime!=null) {
				String currentDate = HelperUtil.getCurrentDate();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date ct = sdf.parse(createtime);
				Date cd = sdf.parse(currentDate);
				dur = (cd.getTime() - ct.getTime()) / 1000;
			}
			
			String telephone = "";
			if (!"".equals(ret.get("telephone")) && ret.get("telephone")!=null) {
				telephone=ret.get("telephone").toString();
			}
			BrokerBean info = brokerMapper.getBrokerInfo(popid);
			Broker authenInfo = brokerMapper.authenInfo(popid);
			//是否认证
			if (authenInfo.getIsAuthen()==HAS_AUTHENED) {
				// 如果没有该订单的抢单记录则可以抢单
				if ((Long) ret.get("count") < OrderCons.ONE_ROW) {
					if (dur > LIMET_COMPET_TIME) {
						if (!info.getTelephone().equals(telephone)) {
							// 抢单修改订单状态
							int insertRows = orderMapper.addCompetBroker(params);
							orderMapper.updateCompetStatus(params);
							return insertRows;
						} else {
							// 不能抢自己发布的订单
							return OrderCons.LIMET_ORDER_BY_SELF;
						}
					} else {
						// 在限制抢单时间内抢单
						return OrderCons.LIMET_COMPET_TIME;
					}
				} else {
					// 已抢单
					return OrderCons.HAS_COMPETED;
				}
			}else if(authenInfo.getIsAuthen()==IS_NOT_AUTHENED) {
				// 未认证
				return IS_NOT_AUTHENED;
			}else if (authenInfo.getIsAuthen()==AUTHENING) {
				return AUTHENING;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return OrderCons.FAIL;
	}

	/**
	 * 已购买
	 * 
	 * @param popid
	 * @param orderid
	 */
	public int purchased(int popid, int orderid, double fee,int adverttype) {
		OrderInfoBean order = orderMapper.getOrder(orderid);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", OrderCons.PURCHASED);
		params.put("popid", popid);
		params.put("orderid", orderid);
		params.put("preStatus", OrderCons.ACCEPTED);
		params.put("fee", fee);
		params.put("adverttype", adverttype);
		params.put("advertId", order.getAdvertId());
		params.put("createtime", HelperUtil.getCurrentDate());
		int result = orderMapper.purchased(params);
		return result;
	}

	/**
	 * 已办完
	 * 
	 * @param popid
	 * @param orderid
	 */
	public int finished(int popid, int orderid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", OrderCons.FINISHED);
		params.put("popid", popid);
		params.put("orderid", orderid);
		params.put("preStatus", OrderCons.ACCEPTED);
		params.put("createtime", HelperUtil.getCurrentDate());
		int result = orderMapper.updateStatus(params);
		return result;
	}

	/**
	 * 已取货
	 * 
	 * @param popid
	 * @param orderid
	 */
	public int collected(int popid, int orderid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", OrderCons.COLLECTED);
		params.put("popid", popid);
		params.put("orderid", orderid);
		params.put("preStatus", OrderCons.ACCEPTED);
		params.put("createtime", HelperUtil.getCurrentDate());
		int result = orderMapper.updateStatus(params);
		return result;
	}

	public List<OrderMap> home(Page page, int type) {
		List<OrderMap> orderlists = null;
		System.out.println(page.getPopid());
		if (type == 0) {
			if (page.getStartid() == 0) {
				orderlists = orderMapper.home_refresh(page);
			} else {
				orderlists = orderMapper.home_load(page);
			}
		} else {
//			Map<String, Object> m = new HashMap<String, Object>();
//			m.put("startid", page.getStartid());
//			m.put("size", page.getSize());
//			m.put("type", type);
//			m.put("popid", page.getPopid());
//			m.put("cityname", page.getCityname());
//			m.put("lat", page.getLat());
//			m.put("lng", page.getLng());
			if (page.getStartid() == 0) {
				orderlists = orderMapper.home1_refresh(page);
			} else {
				orderlists = orderMapper.home1_load(page);
			}
		}
		for (OrderMap om : orderlists) {
			if (om.getPicpath() == null || om.getPicpath().equals(""))
				;
			else {
				om.setPicpathList(om.getPicpath().split(","));
			}
			om.setPicpath(null);
		}
		return orderlists;
	}

	public Data countPage(Page page, int type) {
		if (type == 0)
			return orderMapper.count_page(page);
		else {
//			Map<String, Object> m = new HashMap<String, Object>();
//			m.put("startid", page.getStartid());
//			m.put("size", page.getSize());
//			m.put("type", type);
//			m.put("popid", page.getPopid());
//			m.put("cityname", page.getCityname());
//			m.put("lat", page.getLat());
//			m.put("lng", page.getLng());
			return orderMapper.count_page1(page);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.OrderService#taskList(int)
	 */
	public List<TaskBean> taskList(Map<String, Object> params) {
		return orderMapper.taskList(params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.OrderService#getUserByOrderId(int)
	 */
	public String getUserByOrderId(int orderid) {
		return orderMapper.getUserByOrderId(orderid);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.OrderService#getOrderInfo(int)
	 */
	public OrderInfoBean getOrderInfo(int orderid) {
		OrderInfoBean order = orderMapper.getOrder(orderid);
		String picpath = order.getPicpath();
		if (picpath != null && !"".equals(picpath)) {
			String[] paths = picpath.split(",");
			order.setPicpaths(paths);
		}
		return order;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.OrderService#getOrderInfo(int)
	 */
	public OrderInfoV1_1Bean getOrderInfoForV1_1(int orderid) {
		OrderInfoV1_1Bean order = orderMapper.getOrderForV1_1(orderid);
		String picpath = order.getPicpath();
		if (picpath != null && !"".equals(picpath)) {
			String[] paths = picpath.split(",");
			order.setPicpaths(paths);
		}
		return order;
	}
	
	/* (non-Javadoc)
	 * @see me.send.broker.service.OrderService#getOrderInfoForV1_1_1(int)
	 */
	public OrderInfoV1_1_1Bean getOrderInfoForV1_1_1(int orderid) {
		OrderInfoV1_1_1Bean order = orderMapper.getOrderForV1_1_1(orderid);
		String picpath = order.getPicpath();
		if (picpath != null && !"".equals(picpath)) {
			String[] paths = picpath.split(",");
			order.setPicpaths(paths);
		}
		return order;
	}
	
	/* (non-Javadoc)
	 * @see me.send.broker.service.OrderService#getOrderInfoForV1_1_1(int)
	 */
	public OrderInfoV1_1_2Bean getOrderInfoForV1_1_2(int orderid) {
		OrderInfoV1_1_2Bean order = orderMapper.getOrderForV1_1_2(orderid);
		String picpath = order.getPicpath();
		if (picpath != null && !"".equals(picpath)) {
			String[] paths = picpath.split(",");
			order.setPicpaths(paths);
		}
		return order;
	}
	
	/**
	 * 经纪人同意取消订单
	 * 
	 * @param orderid
	 * @return
	 */
	public int deleteOrder(int orderid, int brokerid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderid", orderid);
		params.put("brokerid", brokerid);
		// 需要加入条件判断
		OrderInfoV1_1_1Bean order = orderMapper.getOrderForV1_1_1(orderid);
		if(order.getNeedCancel()!=1){
			return 0;
		}
		if(order.getStatus()==8||order.getStatus()==9||order.getStatus()==1||order.getStatus()==2){
			return 0;
		}
		return orderMapper.deleteOrder(params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.OrderService#completedRecord(int)
	 */
	public List<TaskBean> completedRecord(int brokerid, int startid, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("brokerid", brokerid);
		params.put("startid", startid);
		params.put("size", size);
		return orderMapper.completedRecord(params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.OrderService#getUserIdByOrderId(int)
	 */
	public int getUserIdByOrderId(int orderid) {
		return orderMapper.getUserIdByOrderId(orderid);
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.OrderService#getUserByOrder(int)
	 */
	public User getUserByOrder(int orderid) {
		return orderMapper.getUserByOrder(orderid);
	}

}
